package ytl;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.text.ParsePosition;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class Inventory {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    String supply = new String();
    JComboBox search = new JComboBox();
    int ctr;
    int i;
    String Button_Command = "Add";
    public static Main_Menu Menu = new Main_Menu();
    JFrame Inventory_Frame = new JFrame("Add");
    JTextArea Summary = new JTextArea();
    JLabel Error_frame = new JLabel("");
    JLabel Error_inventory = new JLabel("");
    Dimension D = new Dimension();
    Dimension E = new Dimension();
    
    JTextField Name_txt = new JTextField("");
    JTextField Category_txt = new JTextField("");
    JTextField Size_txt = new JTextField("");
    JTextField Color_txt = new JTextField("");
    JTextField Buy_Price_txt = new JTextField("");
    JTextField Picture_txt = new JTextField("");
    JTextField Quantity_txt = new JTextField("");
    JTextField Price_txt = new JTextField("");
    
    final static String Header[][] = {{"Name", "Category", "Size", "Color", "Buy Price", "Picture", "Quantity", "Price"}};
    final static String Insider[] = {"","","","","","","",""};
        
    final static DefaultTableModel Inventory_model = new DefaultTableModel(Header,Insider);
    final static DefaultTableModel Inventory_disc_model = new DefaultTableModel(Header,Insider);
    String test;
    Component comp;
    JScrollPane Inventory_scrollpane = new JScrollPane();
    JScrollPane scrollpane = new JScrollPane();
    
    JTable Inventory_table = new JTable(Inventory_model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;
        }
        @Override
        public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
            comp = super.prepareRenderer(renderer, Index_row, Index_col);

            test =  Inventory_table.getModel().getValueAt(Index_row, 0 ).toString();
            
                if(Index_row == 0){
                    comp.setBackground(Color.black);
                    comp.setForeground(Color.white);
                }
                else if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
                    comp.setBackground(Color.gray);
                    comp.setForeground(Color.white);
                }
                else if (Index_row % 2 == 1 && !isCellSelected(Index_row, Index_col)) {
                    comp.setBackground(Color.white);
                    comp.setForeground(Color.black);
                } 
                else {
                    comp.setBackground(Color.RED);
                    comp.setForeground(Color.white);
                }
            return comp;
        }
    };
    
    ImageIcon search_product_button_img = new ImageIcon("pictures/inventory/search_product_button.png");
    ImageIcon search_product_buttonr_img = new ImageIcon("pictures/inventory/search_product_button_r.png");
    ImageIcon search_product_buttonp_img = new ImageIcon("pictures/inventory/search_product_button_p.png");
        
    ImageIcon supply_product_button_img = new ImageIcon("pictures/inventory/supply_product_button.png");
    ImageIcon supply_product_buttonr_img = new ImageIcon("pictures/inventory/supply_product_button_r.png");
    ImageIcon supply_product_buttonp_img = new ImageIcon("pictures/inventory/supply_product_button_p.png");
        
    ImageIcon discontinue_product_button_img = new ImageIcon("pictures/inventory/discontinue_product_button.png");
    ImageIcon discontinue_product_buttonr_img = new ImageIcon("pictures/inventory/discontinue_product_button_r.png");
    ImageIcon discontinue_product_buttonp_img = new ImageIcon("pictures/inventory/discontinue_product_button_p.png");

    ImageIcon edit_product_button_img = new ImageIcon("pictures/inventory/edit_product_button.png");
    ImageIcon edit_product_buttonr_img = new ImageIcon("pictures/inventory/edit_product_button_r.png"); 
    ImageIcon edit_product_buttonp_img = new ImageIcon("pictures/inventory/edit_product_button_p.png"); 

    ImageIcon add_product_button_img = new ImageIcon("pictures/inventory/add_product_button.png");
    ImageIcon add_product_buttonr_img = new ImageIcon("pictures/inventory/add_product_button_r.png");
    ImageIcon add_product_buttonp_img = new ImageIcon("pictures/inventory/add_product_button_p.png");

    ImageIcon confirm_button_img = new ImageIcon("pictures/n_confirm_button.png");
    ImageIcon confirm_buttonr_img = new ImageIcon("pictures/n_confirm_button_r.png");
    ImageIcon confirm_buttonp_img = new ImageIcon("pictures/n_confirm_button_p.png");
    
    ImageIcon return_button_img = new ImageIcon("pictures/n_return_button.png");
    ImageIcon return_buttonr_img = new ImageIcon("pictures/n_return_button_r.png");
    ImageIcon return_buttonp_img = new ImageIcon("pictures/n_return_button_p.png");
    
    ImageIcon product_added_icon = new ImageIcon("pictures/success_product_added.png");
    ImageIcon product_discontinued_icon = new ImageIcon("pictures/success_product_discontinued.png");
    ImageIcon product_continued_icon = new ImageIcon("pictures/success_product_continued.png");
    ImageIcon product_edited_icon = new ImageIcon("pictures/success_product_edited.png");
    
    ImageIcon already_discontinued_error_icon = new ImageIcon("pictures/error/error_already_discontinued.png");
    ImageIcon supply_product_error_icon = new ImageIcon("pictures/error/error_select_supply_row.png");
    ImageIcon edit_product_error_icon = new ImageIcon("pictures/error/error_select_edit_row.png");
    ImageIcon discontinue_product_error_icon = new ImageIcon("pictures/error/error_select_discontinue_row.png");
    ImageIcon blank_fields_error_icon = new ImageIcon("pictures/error/error_complete_blank_fields.png");
    ImageIcon invalid_input_error_icon = new ImageIcon("pictures/error/error_not_valid.png");
    ImageIcon frame_spacer = new ImageIcon("pictures/error/gray.png");
    
    JButton Return_Button = new JButton(return_button_img);
    JButton Confirm_Button = new JButton(confirm_button_img);
    JButton Add_Button = new JButton(add_product_button_img);
    JButton Edit_Button = new JButton(edit_product_button_img);
    JButton Delete_Button = new JButton(discontinue_product_button_img);
    JButton Supply_Button = new JButton(supply_product_button_img);
    JButton Search_Button = new JButton(search_product_button_img);
        
    Object Information_Converter = new Object();
    String Information_Handler = new String();
    JLabel logo = new JLabel();
    
    Box Inventory_Tables_box = Box.createVerticalBox();
    Box Inventory_Data_box = Box.createVerticalBox();
    
    
    JLabel Error_logo = new JLabel();
    JFrame Error_Frame = new JFrame("Error !!");
    Container Error_contentPane = Error_Frame.getContentPane();
    
    JLabel Type_Command = new JLabel("");
    JLabel Name_label = new JLabel("Name:");
    JLabel Category_label = new JLabel("Category:");        
    JLabel Size_label = new JLabel("Size:");
    JLabel Color_label = new JLabel("Color:");
    JLabel Buy_Price_label = new JLabel("Buy Price:");
    JLabel Picture_label = new JLabel("Picture:");
    JLabel Quantity_label = new JLabel("Quantity:");
    JLabel Price_label = new JLabel("Price:");
        
    String str = new String();
    Point pt = new Point();
    Container Inventory_Frame_container = Inventory_Frame.getContentPane();
     
    Box frame_template_box2 = Box.createHorizontalBox();
    Box frame_template_box = Box.createVerticalBox();
    Box Name_box = Box.createHorizontalBox();
    Box Category_box = Box.createHorizontalBox();
    Box Size_box = Box.createHorizontalBox();
    Box Color_box = Box.createHorizontalBox();
    Box Buy_Price_box = Box.createHorizontalBox();
    Box Picture_box = Box.createHorizontalBox();
    Box Quantity_box = Box.createHorizontalBox();
    Box Price_box = Box.createHorizontalBox();
    Box Error_box = Box.createHorizontalBox();
    Box search_box = Box.createHorizontalBox();
    Box search_field_box = Box.createVerticalBox();
    Box Summary_box = Box.createVerticalBox();
    JTextField search_field = new JTextField();
    String Query = new String();
    ResultSet Result = null;
    Dimension X = new Dimension();
    
    // </editor-fold> 
    
    public void Init_Inventory(){
        search.addItem("Name");search.addItem("Category"); search.addItem("Size");search.addItem("Color"); 
        search_field_box.add(search);
        search_field_box.add(Box.createHorizontalStrut(10));
        search_field_box.add(search_field);
        search_box.add(search_field_box);
        
        
        Summary.setEditable(false);
        Summary.setFont(new java.awt.Font("Verdana", 1, 14));
        Summary.setBackground(Color.lightGray);
        X.setSize(300,220);
        Summary.setMinimumSize(X);
        Summary.setMaximumSize(X);
        scrollpane.setMinimumSize(X);
        scrollpane.setMaximumSize(X);
        Summary_box.add(scrollpane);
        frame_template_box2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        frame_template_box2.add(Box.createVerticalStrut(10));
        frame_template_box2.add(Confirm_Button);
        frame_template_box2.add(Box.createVerticalStrut(10));
        frame_template_box2.add(Return_Button);
        frame_template_box2.add(Box.createVerticalStrut(10));
        
        Inventory_table.setRowHeight(24);
        try{
        try{Result = Main_Menu.DBConnection.Query_Retrieve("Select * from product");} catch (ClassNotFoundException ex) {} catch (SQLException ex) {} catch (NullPointerException ex) {}
        try{while(Result.next())
                if(Result.getString(13).equals("Available"))
                    Inventory_model.insertRow(Inventory_model.getRowCount(),new Object[]{Result.getString(3), Result.getString(4), Result.getString(5), Result.getString(6), "Php " + Result.getString(7), Result.getString(10) , Result.getString(11), "Php " + Result.getString(8)});
                else
                    Inventory_disc_model.insertRow(Inventory_disc_model.getRowCount(),new Object[]{Result.getString(3), Result.getString(4), Result.getString(5), Result.getString(6), "Php " + Result.getString(7), Result.getString(10) , Result.getString(11), "Php " + Result.getString(8)});
        } catch (SQLException ex) {Error();} catch (NullPointerException ex) {Error();}
        Main_Menu.DBConnection.DBClose();
        }catch (NullPointerException ex) {Error();}
        
        Error_frame.setFont(new java.awt.Font("Verdana", 0, 30));
        frame_template_box.setBorder(BorderFactory.createEmptyBorder(15, 15, 0, 20));
        Error_Frame.setSize(new Dimension(280, 130));
        Error_Frame.setResizable(false);
        Error_logo.setIcon(new javax.swing.ImageIcon("pictures/error/error_general.png"));
        Error_contentPane.setBackground(Color.white);
        Error_contentPane.add(Error_logo,BorderLayout.BEFORE_LINE_BEGINS);
        Error_inventory.setForeground(Color.red);
        Error_frame.setForeground(Color.red);
        
        logo.setIcon(new javax.swing.ImageIcon("pictures/inventory/YTL Logo (inventory250).png"));
       
        Name_label.setFont(new java.awt.Font("Verdana", 1, 12));
        Category_label.setFont(new java.awt.Font("Verdana", 1, 12));
        Size_label.setFont(new java.awt.Font("Verdana", 1, 12));
        Color_label.setFont(new java.awt.Font("Verdana", 1, 12));
        Buy_Price_label.setFont(new java.awt.Font("Verdana", 1, 12));
        Picture_label.setFont(new java.awt.Font("Verdana", 1, 12));
        Quantity_label.setFont(new java.awt.Font("Verdana", 1, 12));
        Price_label.setFont(new java.awt.Font("Verdana", 1, 12));
        
        D.setSize(170,20);
        Name_txt.setMinimumSize(D);
        Category_txt.setMinimumSize(D);
        Size_txt.setMinimumSize(D);
        Color_txt.setMinimumSize(D);
        Buy_Price_txt.setMinimumSize(D);
        Picture_txt.setMinimumSize(D);
        Quantity_txt.setMinimumSize(D);
        Price_txt.setMinimumSize(D);
        
        Name_txt.setMaximumSize(D);
        Category_txt.setMaximumSize(D);
        Size_txt.setMaximumSize(D);
        Color_txt.setMaximumSize(D);
        Buy_Price_txt.setMaximumSize(D);
        Picture_txt.setMaximumSize(D);
        Quantity_txt.setMaximumSize(D);
        Price_txt.setMaximumSize(D);
    
        E.setSize(80,20);
        Name_label.setMinimumSize(E);
        Category_label.setMinimumSize(E);
        Size_label.setMinimumSize(E);
        Color_label.setMinimumSize(E);
        Buy_Price_label.setMinimumSize(E);
        Picture_label.setMinimumSize(E);
        Quantity_label.setMinimumSize(E);
        Price_label.setMinimumSize(E);
        
        Name_label.setMaximumSize(E);
        Category_label.setMaximumSize(E);
        Size_label.setMaximumSize(E);
        Color_label.setMaximumSize(E);
        Buy_Price_label.setMaximumSize(E);
        Picture_label.setMaximumSize(E);
        Quantity_label.setMaximumSize(E);
        Price_label.setMaximumSize(E);
        
        Return_Button.setRolloverIcon(return_buttonr_img);
        Return_Button.setPressedIcon(return_buttonp_img);
        Return_Button.setActionCommand("Return");
        Return_Button.addActionListener(new Return());
        Main_Menu.Button_Options(Return_Button);
        
        Confirm_Button.setRolloverIcon(confirm_buttonr_img);
        Confirm_Button.setPressedIcon(confirm_buttonp_img);
        Confirm_Button.setActionCommand("Confirm");
        Confirm_Button.addActionListener(new Confirm());
        Main_Menu.Button_Options(Confirm_Button);
        
        Name_box.add(Name_label);
        Name_box.add(Name_txt);
        Category_box.add(Category_label);
        Category_box.add(Category_txt);
        Size_box.add(Size_label);
        Size_box.add(Size_txt);
        Color_box.add(Color_label);
        Color_box.add(Color_txt);
        Buy_Price_box.add(Buy_Price_label);
        Buy_Price_box.add(Buy_Price_txt);
        Picture_box.add(Picture_label);
        Picture_box.add(Picture_txt);
        Quantity_box.add(Quantity_label);
        Quantity_box.add(Quantity_txt);
        Price_box.add(Price_label);
        Price_box.add(Price_txt);
        Error_box.add(Error_frame);
        
        Add_Button.setRolloverIcon(add_product_buttonr_img);
        Add_Button.setPressedIcon(add_product_buttonp_img);
        Add_Button.setActionCommand("Add Product");
        Add_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Add_Button);
        
        Edit_Button.setRolloverIcon(edit_product_buttonr_img);
        Edit_Button.setPressedIcon(edit_product_buttonp_img);
        Edit_Button.setActionCommand("Edit Product");
        Edit_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Edit_Button);
        
        Delete_Button.setRolloverIcon(discontinue_product_buttonr_img);
        Delete_Button.setPressedIcon(discontinue_product_buttonp_img);
        Delete_Button.setActionCommand("Discontinue Product");
        Delete_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Delete_Button);
        
        Supply_Button.setRolloverIcon(supply_product_buttonr_img);
        Supply_Button.setPressedIcon(supply_product_buttonp_img);
        Supply_Button.setActionCommand("Supply Product");
        Supply_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Supply_Button);
        
        Search_Button.setRolloverIcon(search_product_buttonr_img);
        Search_Button.setPressedIcon(search_product_buttonp_img);
        Search_Button.setActionCommand("Search Product");
        Search_Button.addActionListener(new Action());
        Main_Menu.Button_Options(Search_Button);
            
        scrollpane.setViewportView(Summary);
        frame_template("Add");
    }
    
    protected JComponent Inventory_Data() {
        Inventory_Data_box.removeAll();
        Inventory_Data_box.add(logo);
        Inventory_Data_box.add(Box.createVerticalStrut(5));
        Inventory_Data_box.add(Add_Button);
        Inventory_Data_box.add(Box.createVerticalStrut(5));
        Inventory_Data_box.add(Edit_Button);
        Inventory_Data_box.add(Box.createVerticalStrut(5));
        Inventory_Data_box.add(Delete_Button);
        Inventory_Data_box.add(Box.createVerticalStrut(5));
        Inventory_Data_box.add(Supply_Button);
        Inventory_Data_box.add(Box.createVerticalStrut(5));
        Inventory_Data_box.add(Search_Button);
        Inventory_Data_box.add(Box.createVerticalStrut(5));
        Inventory_Data_box.add(Error_inventory);        
        return Inventory_Data_box;
    }
    
    protected JComponent Inventory_Tables() {
        
        Inventory_Tables_box.removeAll();
        Inventory_Tables_box.add(Box.createVerticalStrut(5));
        Inventory_Tables_box.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

        Inventory_scrollpane.setViewportView(Inventory_table);

        Inventory_Tables_box.add(Inventory_scrollpane);
        return Inventory_Tables_box;
    }
    
    public class Action implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae){
            str = ae.getActionCommand();
                     
            Reload();
            if(str.equals("Add Product")){
                Button_Command = "Add";
                frame_template("Add");
                Inventory_Frame.setTitle(Button_Command);
                Inventory_Frame.setVisible(true);
            }
            else if(str.equals("Edit Product")){
                if (0 >= Inventory_table.getSelectedRow()){
                    Error_inventory.setIcon(edit_product_error_icon);
                    Error();
                    Main_Menu.frame.getContentPane().repaint();
                }
                else {
                    Button_Command = "Edit";
                    frame_template("Edit");
                    Inventory_Frame.setTitle(Button_Command);
                    Inventory_Frame.repaint();
                    Inventory_Frame.setVisible(true);
                    
                    Name_txt.setText(Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 0).toString());
                    Category_txt.setText(Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 1).toString());
                    Size_txt.setText(Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 2).toString());
                    Color_txt.setText(Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 3).toString());
                    Buy_Price_txt.setText(Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 4).toString().substring(4));
                    Picture_txt.setText(Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 5).toString());
                    Quantity_txt.setText(Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 6).toString());
                    Price_txt.setText(Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 7).toString().substring(4));
   
                }
            }
            else if(str.equals("Discontinue Product")){
                if (0 >= Inventory_table.getSelectedRow()){
                    Error();
                    Error_inventory.setIcon(discontinue_product_error_icon);
                    Main_Menu.frame.getContentPane().repaint();
                }
                else {
                String test = Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 0).toString();
                    if(test.startsWith("*")){
                        Error();
                        Error_inventory.setIcon(already_discontinued_error_icon);
                        Main_Menu.frame.getContentPane().repaint();
                    }
                    else{
                        Button_Command = "Discontinue";
                        frame_template("Discontinue");
                        Inventory_Frame.setTitle(Button_Command);
                        Inventory_Frame.repaint();
                       Inventory_Frame.setVisible(true);
                    }   
                }
            }
            else if(str.equals("Supply Product")){
                if (0 >= Inventory_table.getSelectedRow()){
                    Error();
                    Error_inventory.setIcon(supply_product_error_icon);
                Main_Menu.frame.getContentPane().repaint();
                }
                else {Button_Command = "Supply";
                frame_template("Supply");
                Inventory_Frame.setTitle(Button_Command);
                Inventory_Frame.repaint();
                Inventory_Frame.setVisible(true);
                }
            }
            else if(str.equals("Search Product")){
                Button_Command = "Search";
                frame_template("Search");
                Inventory_Frame.setTitle(Button_Command);
                Inventory_Frame.repaint();
                Inventory_Frame.setVisible(true);
            }
        }
    }
//    int x;
    public void Reload(){
//        x = Inventory_model.getRowCount();
//        for(ctr = 0; ctr < x - 1; ctr++)
//            Inventory_model.removeRow(1);
//        
//        try{Result = Main_Menu.DBConnection.Query_Retrieve("Select * from product");} catch (ClassNotFoundException ex) {} catch (SQLException ex) {} catch (NullPointerException ex) {}
//        try{while(Result.next())
//                if(Result.getString(13).equals("Available"))
//                    Inventory_model.insertRow(Inventory_model.getRowCount(),new Object[]{Result.getString(3), Result.getString(4), Result.getString(5), Result.getString(6), "Php " + Result.getString(7), Result.getString(10) , Result.getString(11), "Php " + Result.getString(8)});
//        } catch (SQLException ex) {} catch (NullPointerException ex) {}
//        Main_Menu.DBConnection.DBClose();
        
        
        Error_inventory.setText(""); 
        Error_frame.setText(" ");
        Error_inventory.setIcon(null);
        Error_frame.setIcon(frame_spacer);
        Summary.setText("");
        
        Name_txt.setText("");
        Category_txt.setText("");
        Size_txt.setText("");
        Color_txt.setText("");
        Buy_Price_txt.setText("");
        Picture_txt.setText("");
        Quantity_txt.setText("");
        Price_txt.setText("");
               
        Name_txt.setBackground(Color.white); 
        Category_txt.setBackground(Color.white);
        Size_txt.setBackground(Color.white);
        Color_txt.setBackground(Color.white);
        Buy_Price_txt.setBackground(Color.white);
        Picture_txt.setBackground(Color.white);
        Quantity_txt.setBackground(Color.white);
        Price_txt.setBackground(Color.white);
        
        Inventory_table.repaint();        
        Inventory_Frame.repaint();
        Inventory_Frame_container.repaint();
    }
    
    public class Confirm implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae){
            str = ae.getActionCommand();
            
            Error_inventory.setText(""); 
            Error_frame.setText(" ");
            Error_inventory.setIcon(null);
            Error_frame.setIcon(frame_spacer);
            Summary.setText("");

            Name_txt.setBackground(Color.white); 
            Category_txt.setBackground(Color.white);
            Size_txt.setBackground(Color.white);
            Color_txt.setBackground(Color.white);
            Buy_Price_txt.setBackground(Color.white);
            Picture_txt.setBackground(Color.white);
            Quantity_txt.setBackground(Color.white);
            Price_txt.setBackground(Color.white);

            Inventory_table.repaint();
            Inventory_Frame.repaint();
            Inventory_Frame_container.repaint();
        
            if(Button_Command.equals("Add")){
                if(Name_txt.getText().equals("") || Category_txt.getText().equals("") || Buy_Price_txt.getText().equals("") || Quantity_txt.getText().equals("") || Price_txt.getText().equals("")){
                    if(Name_txt.getText().equals(""))Name_txt.setBackground(Color.pink); 
                    if(Category_txt.getText().equals(""))Category_txt.setBackground(Color.pink);
                    if(Buy_Price_txt.getText().equals(""))Buy_Price_txt.setBackground(Color.pink);
                    if(Quantity_txt.getText().equals(""))Quantity_txt.setBackground(Color.pink);
                    if(Price_txt.getText().equals(""))Price_txt.setBackground(Color.pink);
                    Error_frame.setIcon(blank_fields_error_icon);
                    Error();                      
                }
                else if(!isNumeric(Buy_Price_txt.getText()) || !isNumeric(Quantity_txt.getText()) || !isNumeric(Price_txt.getText()) || Float.valueOf(Buy_Price_txt.getText()) <= 0 || Math.round(Float.valueOf(Quantity_txt.getText())) <= 0 || Float.valueOf(Price_txt.getText()) <= 0)
                {
                    if(!isNumeric(Buy_Price_txt.getText()) || Float.valueOf(Buy_Price_txt.getText()) <= 0)Buy_Price_txt.setBackground(Color.pink);
                    if(!isNumeric(Quantity_txt.getText()) || Math.round(Float.valueOf(Quantity_txt.getText())) <= 0)Quantity_txt.setBackground(Color.pink);
                    if(!isNumeric(Price_txt.getText()) || Float.valueOf(Price_txt.getText()) <= 0)Price_txt.setBackground(Color.pink);
                    Error_frame.setIcon(invalid_input_error_icon);
                    Error();
                }
                else{
                    if(Size_txt.getText().equals(""))Size_txt.setText(null);
                    if(Color_txt.getText().equals(""))Color_txt.setText(null);
                    if(Picture_txt.getText().equals(""))Picture_txt.setText("Default.jpg");
                    
                    try{Main_Menu.DBConnection.Query_Send("INSERT INTO `[ytl]`.`product` (`Name`, `Category`, `Size`, `Color`, `PurchasePrice`, `Picture`, `Quantity`, `SellingPrice`, `Tax`) VALUES ( '" + Name_txt.getText() + "', '" + Category_txt.getText() + "', '" + Size_txt.getText() + "', '" + Color_txt.getText() + "', '" + Buy_Price_txt.getText() + "', '" + Picture_txt.getText() + "', '" + Math.round(Float.valueOf(Quantity_txt.getText())) + "', '" + Price_txt.getText() + "', '" + (Float.parseFloat(Price_txt.getText())*Main_Menu.DBConnection.Tax) + "');");
                    Main_Menu.DBConnection.DBClose();
                    
                    Inventory_Frame.setVisible(false);
                    Error_frame.setIcon(product_added_icon);
                    
                    Inventory_model.insertRow(Inventory_model.getRowCount(),new Object[]{Name_txt.getText(),Category_txt.getText(),Size_txt.getText(),Color_txt.getText(),"Php " + String.format("%.2f",Float.parseFloat(Buy_Price_txt.getText())),Picture_txt.getText(),Math.round(Float.valueOf(Quantity_txt.getText())),"Php " + String.format("%.2f",Float.parseFloat(Price_txt.getText()))});
                    Reload();
                    } catch (ClassNotFoundException ex) {Error();} catch (SQLException ex) {Error();} catch (NullPointerException ex) {Error();}
                }
            }
            else if(Button_Command.equals("Edit")){
                if(Name_txt.getText().equals("") || Category_txt.getText().equals("") || Buy_Price_txt.getText().equals("")  || Quantity_txt.getText().equals("") || Price_txt.getText().equals("")){
                    if(Name_txt.getText().equals(""))Name_txt.setBackground(Color.pink); 
                    if(Category_txt.getText().equals(""))Category_txt.setBackground(Color.pink);
                    if(Buy_Price_txt.getText().equals(""))Buy_Price_txt.setBackground(Color.pink);
                    if(Quantity_txt.getText().equals(""))Quantity_txt.setBackground(Color.pink);
                    if(Price_txt.getText().equals(""))Price_txt.setBackground(Color.pink);
                    Error_frame.setIcon(blank_fields_error_icon);
                    Error();
                }
                else if(!isNumeric(Buy_Price_txt.getText()) || !isNumeric(Quantity_txt.getText()) || !isNumeric(Price_txt.getText()) || Float.valueOf(Buy_Price_txt.getText()) <= 0 || Math.round(Float.valueOf(Quantity_txt.getText())) <= 0 || Float.valueOf(Price_txt.getText()) <= 0)
                {
                    if(!isNumeric(Buy_Price_txt.getText()) || Float.valueOf(Buy_Price_txt.getText()) <= 0)Buy_Price_txt.setBackground(Color.pink);
                    if(!isNumeric(Quantity_txt.getText()) || Math.round(Float.valueOf(Quantity_txt.getText())) <= 0)Quantity_txt.setBackground(Color.pink);
                    if(!isNumeric(Price_txt.getText()) || Float.valueOf(Price_txt.getText()) <= 0)Price_txt.setBackground(Color.pink);
                    Error_frame.setIcon(invalid_input_error_icon);
                    Error();
                }
            else{
                    if(Size_txt.getText().equals(""))Size_txt.setText(null);
                    if(Color_txt.getText().equals(""))Color_txt.setText(null);
                    if(Picture_txt.getText().equals(""))Picture_txt.setText("Default.jpg");
                    
                    try{Main_Menu.DBConnection.Query_Send("UPDATE product SET Name = '"+ Name_txt.getText() +"', Category = '"+ Category_txt.getText() +"', Size = '"+ Size_txt.getText() +"', Color = '"+ Color_txt.getText() +"', PurchasePrice = '"+ Buy_Price_txt.getText() +"', SellingPrice = '"+ Price_txt.getText() +"', Picture = '"+ Picture_txt.getText() +"', Quantity = '"+ Math.round(Float.valueOf(Quantity_txt.getText())) +"' WHERE product.Name ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 0).toString() +"' AND product.Category ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 1).toString() +"' AND product.Size ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 2).toString() +"' AND product.Color ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 3).toString() +"'AND product.PurchasePrice ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 4).toString().substring(4) +"' AND product.Picture ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 5).toString() +"'AND product.Quantity ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 6).toString() +"'AND product.SellingPrice ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 7).toString().substring(4) +"';");
                    Main_Menu.DBConnection.DBClose();
                    
                    Inventory_model.setValueAt( Name_txt.getText() , Inventory_table.getSelectedRow(), 0);
                    Inventory_model.setValueAt( Category_txt.getText() , Inventory_table.getSelectedRow(), 1);
                    Inventory_model.setValueAt( Size_txt.getText() , Inventory_table.getSelectedRow(), 2);
                    Inventory_model.setValueAt( Color_txt.getText() , Inventory_table.getSelectedRow(), 3);
                    Inventory_model.setValueAt( "Php " + Buy_Price_txt.getText() , Inventory_table.getSelectedRow(), 4);
                    Inventory_model.setValueAt( Picture_txt.getText() , Inventory_table.getSelectedRow(), 5);
                    Inventory_model.setValueAt( Math.round(Float.valueOf(Quantity_txt.getText())) , Inventory_table.getSelectedRow(), 6);
                    Inventory_model.setValueAt( "Php " + Price_txt.getText(), Inventory_table.getSelectedRow(), 7);
                    Reload();
                    Inventory_Frame.setVisible(false);
                    
                    Error_frame.setIcon(product_edited_icon);
                    } catch (ClassNotFoundException ex) {Error();} catch (SQLException ex) {Error();} catch (NullPointerException ex) {Error();}
                }
            }
            else if(Button_Command.equals("Discontinue")){
                if(Inventory_table.getSelectedRow() > 0 ){                        
                try{Main_Menu.DBConnection.Query_Send("UPDATE product SET Status = 'Discontinued' WHERE product.Name ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 0).toString() +"' AND product.Category ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 1).toString() +"' AND product.Size ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 2).toString() +"' AND product.Color ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 3).toString() +"'AND product.PurchasePrice ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 4).toString().substring(4) +"' AND product.Picture ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 5).toString() +"'AND product.Quantity ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 6).toString() +"'AND product.SellingPrice ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 7).toString().substring(4) +"';");
                    Main_Menu.DBConnection.DBClose();
                    
                    Inventory_disc_model.insertRow(Inventory_disc_model.getRowCount(),new Object[]{Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 0), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 1), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 2), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 3), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 4), Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 5),  Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 6) , Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 7)});
                    Inventory_model.removeRow(Inventory_table.getSelectedRow());
                    Reload();
                    Inventory_Frame.setVisible(false);
                    } catch (ClassNotFoundException ex) {Error();} catch (SQLException ex) {Error();} catch (NullPointerException ex) {Error();}    
                }
                    Reload();
                    Inventory_table.repaint();
                    Inventory_Frame.setVisible(false);
                    
                    Error_frame.setIcon(product_discontinued_icon);
            }
            else if(Button_Command.equals("Supply")){
                Inventory_Frame.setVisible(false);
            }
            else if(Button_Command.equals("Search")){
                i = 1;
                if ( 1 <= Inventory_table.getRowCount())
                    while(i < Inventory_table.getRowCount()){
                        try{
                        if(Inventory_table.getValueAt(i, search.getSelectedIndex()).equals(search_field.getText()))
                        {   
                            Inventory.Inventory_model.insertRow(1,new Object[]{Inventory_table.getValueAt(i, 0), Inventory_table.getValueAt(i, 1), Inventory_table.getValueAt(i, 2), Inventory_table.getValueAt(i, 3), Inventory_table.getValueAt(i, 4),Inventory_table.getValueAt(i, 5),Inventory_table.getValueAt(i, 6),Inventory_table.getValueAt(i, 7)});
                            Inventory.Inventory_model.removeRow(i+1);
                        }}catch(NullPointerException e){}
                        i++;
                    }
                Reload();
                Inventory_Frame.setVisible(false);
            }
        }
    }
    public class Return implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent ae){
        Reload();
        Inventory_Frame.setVisible(false);
        
        }
    }
    
    public void frame_template(String Command){
        
        Summary.setText("");
        Inventory_Frame_container.removeAll();
        Inventory_Frame.remove(Type_Command);
        Reload();
                    
        pt = Main_Menu.frame.getLocation();
        Inventory_Frame.setLocation(pt.x + 280,pt.y + 90);
        Inventory_Frame.setSize(new Dimension(330, 335));
        Inventory_Frame.setResizable(false);
        
        if("Supply".equals(Button_Command)||"Discontinue".equals(Button_Command) || "Search".equals(Button_Command)){
            Inventory_Frame.setSize(new Dimension(400, 250));
            Inventory_Frame.setResizable(false);
        }
        if("Search".equals(Button_Command)){
            Inventory_Frame.setSize(new Dimension(330, 250));
            Inventory_Frame.setResizable(false);
        }
        
        frame_template_box.removeAll();
        frame_template_box.add(Type_Command);
        
        frame_template_box.add(Name_box);frame_template_box.add(Box.createHorizontalStrut(10));
        frame_template_box.add(Category_box);frame_template_box.add(Box.createHorizontalStrut(10));
        frame_template_box.add(Size_box);frame_template_box.add(Box.createHorizontalStrut(10));
        frame_template_box.add(Color_box);frame_template_box.add(Box.createHorizontalStrut(10));
        frame_template_box.add(Buy_Price_box);frame_template_box.add(Box.createHorizontalStrut(10));
        frame_template_box.add(Picture_box);frame_template_box.add(Box.createHorizontalStrut(10));
        frame_template_box.add(Quantity_box);frame_template_box.add(Box.createHorizontalStrut(10));
        frame_template_box.add(Price_box);frame_template_box.add(Box.createHorizontalStrut(10));
        
        if("Discontinue".equals(Button_Command)){
            frame_template_box.removeAll();
            Summary.append("\nAre you sure about discontinuing?\n");
            Summary.append("  Name: " + Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 0) + "\n");
            Summary.append("  Category: " + Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 1) + "\n");
            Summary.append("  Size: " + Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 2) + "\n");
            Summary.append("  Color: " + Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 3)+"\n");
            frame_template_box.add(Summary_box);
        }
        if("Supply".equals(Button_Command)){
            frame_template_box.removeAll();
            Summary.append("Supplier Information \n");
            if(Inventory_table.getSelectedRow() > 0 ){
                Summary.append("Item: "+ Inventory_table.getValueAt(Inventory_table.getSelectedRow(), 0) + "\n");
            }
            try{
                Result = null;
                Result = Main_Menu.DBConnection.Query_Retrieve("Select * from product WHERE product.Name ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 0).toString() +"' AND product.Category ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 1).toString() +"' AND product.PurchasePrice ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 4).toString().substring(4) +"' AND product.SellingPrice ='"+ Inventory_model.getValueAt(Inventory_table.getSelectedRow(), 7).toString().substring(4) +"';");
                Result.first();
                supply = Result.getString(2);
                Main_Menu.DBConnection.DBClose();
                Result = null;
                Result = Main_Menu.DBConnection.Query_Retrieve("Select * from supplier WHERE SupplierID ='"+supply+"';");
                Result.first();
                Summary.append(" ID: " + Result.getString(1) + "\n");
                Summary.append(" Name: " + Result.getString(2) + "\n");
                Summary.append(" Contact: " + Result.getString(3) + "\n");
                Summary.append(" Email: " + Result.getString(4) + "\n");
                Summary.append(" Location: " + Result.getString(5));
                Main_Menu.DBConnection.DBClose();
            } catch (ClassNotFoundException ex) {} catch (SQLException ex) {}
            
            frame_template_box.add(Summary_box);
        }
        if("Search".equals(Button_Command)){
            search_field.setText("");
            frame_template_box.removeAll();
            frame_template_box.add(search_box);

        }
        if(!"Supply".equals(Button_Command)&&!"Discontinue".equals(Button_Command))
        frame_template_box.add(Error_box);
        Inventory_Frame_container.setBackground(Color.LIGHT_GRAY);
        Inventory_Frame_container.add(frame_template_box,BorderLayout.BEFORE_LINE_BEGINS);
        Inventory_Frame_container.add(frame_template_box2,BorderLayout.PAGE_END);
        Inventory_Frame_container.repaint();
        Inventory_Frame.repaint();
        
    }
    
    public static void Inventory() {

    }
    
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Inventory();
            }
        });
    }
    
    public void Error(){
        pt = Main_Menu.frame.getLocation();
        Error_Frame.setLocation(pt.x + 280,pt.y + 140);
        try{
        Error_thread threads = new Error_thread();
        threads.start();
        }catch(IllegalThreadStateException e){
        }
    }
    
    class Error_thread extends Thread {
        
        @Override
        public void run() {
            try {
                Error_Frame.setVisible(true);
                if(Main_Menu.Systems.sound==true)
                try {
                AudioClip no = Applet.newAudioClip(new URL("file:pictures/no.wav"));
                no.play();
                } catch (MalformedURLException ex) {
                }
                Thread.sleep(1000);
                Error_Frame.setVisible(false);
                
            } catch (InterruptedException e) {
    }}}
    
    public boolean isNumeric(String inputData) {
          NumberFormat formatter = NumberFormat.getInstance();
          ParsePosition pos = new ParsePosition(0);
          formatter.parse(inputData, pos);
          return inputData.length() == pos.getIndex();
    }
}
